package org.example.inventory.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.example.inventory.common.InventoryException;
import org.example.inventory.entity.Inventory;
import org.example.inventory.mapper.InventoryMapper;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
public class InventoryService {
    private final InventoryMapper inventoryMapper;

    public void lockInventory(String skuId, Integer quantity) {
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<Inventory>().eq(Inventory::getSkuId, skuId);
        Inventory inventory = inventoryMapper.selectOne(queryWrapper);
        if (inventory == null || inventory.getAvailableInventory() - quantity < 0) {
            throw new InventoryException("库存不足");
        }
        inventory.setLockInventory(inventory.getLockInventory() + quantity);
        inventory.setAvailableInventory(inventory.getAvailableInventory() - quantity);
        inventoryMapper.updateById(inventory);
    }

}
